package io.renren.modules.order.controller;

import com.alibaba.fastjson.JSONObject;
import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.order.dto.FileInfoDTO;
import io.renren.modules.order.dto.OrderDTO;
import io.renren.modules.order.dto.OrderPaymentDTO;
import io.renren.modules.order.excel.OrderExcel;
import io.renren.modules.order.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;


/**
 * 订单表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-12-07
 */
@RestController
@RequestMapping("order/order")
@Tag(name="订单表")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
        @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref="int") ,
        @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY,required = true, ref="int") ,
        @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref="String") ,
        @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref="String")
    })
    public Result<PageData<OrderDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params){
        PageData<OrderDTO> page = orderService.orderPage(params);
        return new Result<PageData<OrderDTO>>().ok(page);
    }

    @GetMapping("echarts/payAndNoPay")
    @Operation(summary = "统计已付、未付费用金额")
    public Result<JSONObject> getPayAndNoPayList(@RequestParam Map<String, Object> params){
        JSONObject data = orderService.getPayAndNoPayList(params);
        return new Result<JSONObject>().ok(data);
    }

    @GetMapping("echarts/pay")
    @Operation(summary = "统计已完结订单金额和未完结订单待支付金额")
    public Result<List<JSONObject>> getEchartsPayList(@RequestParam Map<String, Object> params){
        List<JSONObject> list = orderService.getEchartsPayList(params);
        return new Result<List<JSONObject>>().ok(list);
    }

    @GetMapping("echarts/orderNum")
    @Operation(summary = "统计已完结订单数量和未完结订单数量")
    public Result<List<JSONObject>> getEchartsOrderNumList(@RequestParam Map<String, Object> params){
        List<JSONObject> list = orderService.getEchartsOrderNumList(params);
        return new Result<List<JSONObject>>().ok(list);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<OrderDTO> get(@PathVariable("id") Long id){
        OrderDTO data = orderService.getOrder(id);

        return new Result<OrderDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    public Result save(@RequestBody OrderDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        orderService.orderSave(dto);

        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    public Result update(@RequestBody OrderDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        orderService.orderUpdate(dto);
        return new Result();
    }

    @PostMapping("signContract")
    @Operation(summary = "补签合同")
    @LogOperation("补签合同")
    public Result signContract(@RequestBody OrderDTO dto){
        orderService.signContract(dto);
        return new Result();
    }

    @PostMapping("finishOrder")
    @Operation(summary = "订单完结")
    @LogOperation("订单完结")
    public Result finishOrder(@RequestBody OrderDTO dto){
        orderService.finishOrder(dto);
        return new Result();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        orderService.orderDelete(ids);

        return new Result();
    }

    @GetMapping("export")
    @Operation(summary = "导出")
    @LogOperation("导出")
    public void export(@Parameter(hidden = true) @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<OrderExcel> list = orderService.exportList(params);

        ExcelUtils.exportExcelToTarget(response, null, "订单表", list, OrderExcel.class);
    }

    @PostMapping("upload/files")
    @Operation(summary = "批量上传文件")
    public Result<FileInfoDTO> uploadFiles(@RequestParam("file") MultipartFile uploadFile) {
        FileInfoDTO fileInfoDto = orderService.uploadFile(uploadFile);
        return new Result<FileInfoDTO>().ok(fileInfoDto);
    }

}
